package com.wjw.java.oop.polymorphic_;

/**
 * 多态练习题
 */
public class PolymorphicExercise {
    public static void main(String[] args) {
        Sub sub = new Sub();
        //访问成员变量,编译时看=左边,运行时也看=左边
        //=左边是Sub,所以是20
        System.out.println(sub.num);//20
        //访问成员方法,编译时看=左边,运行时看=右边
        //=右边是Sub,所以是20
        sub.printNum();//20
        //父类的引用指向子类对象(向下转型)
        Base base = sub;
        //base和sub都指向堆中的Sub对象,所以为true
        System.out.println(base == sub);//true
        //访问成员变量,编译时看=左边,运行时也看=左边
        //=左边是Base,所以是10
        System.out.println(base.num);//10
        //访问成员方法,编译时看=左边,运行时看=右边
        //=右边是Sub,所以是20
        base.printNum();//20
    }
}
class Base{
    int num = 10;
    public void printNum(){
        System.out.println(this.num);
    }
}
class Sub extends Base{
    int num = 20;
    public void printNum(){
        System.out.println(this.num);
    }
}